package com.jp.springmvc.controller;

import com.jp.springmvc.vo.Spitter;
import com.jp.springmvc.repository.SpitterRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Title: SpitterController
 * Description:
 *
 * @author zifan_jp
 * @version 1.0
 * @since Date: 2019-11-21 17:57
 * Copyright: Copyright (c) zifan_jp
 * ModificationHistory   Who    When    What
 * -----------------------------------------------
 */
@Controller
@RequestMapping("/spitter")
public class SpitterController {

    private final SpitterRepository spitterRepository;
    @Autowired
    public SpitterController(SpitterRepository spitterRepository) {
        this.spitterRepository = spitterRepository;
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String showRegistrationForm() {
        return "registerForm";
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String processRegistration(Spitter spitter){
        spitterRepository.save(spitter);
        return "redirect:/spitter/" + spitter.getUserName();
    }
    @RequestMapping(value = "/{username}",method = RequestMethod.GET)
    public String showSpitterProfile(@PathVariable  String username, Model model){
        model.addAttribute(spitterRepository.findByUsername(username));
        return "profile";
    }
}
